package com.endurance.gateway.filter.factory;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @ProjectName: spring-cloud-hoxton-parent
 * @Package: com.endurance.gateway.filter.factory
 * @ClassName: MultiValuesGatewayFilterFactory
 * @Description: 自定义Config类传递多个参数
 * @Author: sunlin
 * @CreateDate: 2024/02/12 14:57
 * @Version: 1.0
 */

@Component
public class MultiValuesGatewayFilterFactory extends AbstractGatewayFilterFactory<MultiValuesGatewayFilterFactory.Config> {

    public MultiValuesGatewayFilterFactory() {
        super(Config.class);
    }


    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("name", "age", "sex");
    }

    @Override
    public GatewayFilter apply(Config config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                System.out.println("name:" + config.getName());
                System.out.println("age:" + config.getAge());
                System.out.println("sex:" + config.getSex());
                return chain.filter(exchange);
            }
        };
    }

    public static class Config{
        private String name;

        private String age;

        private Boolean sex;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }

        public Boolean getSex() {
            return sex;
        }

        public void setSex(Boolean sex) {
            this.sex = sex;
        }
    }

}
